<?php
include_once "settings.php";
include_once "messages.php";

function my_sort($childs, $i_max){
  for ($i = 0; $i < $i_max - 1; $i++){
    for ($j = 0; $j < $i_max - 1; $j++){
      if (strcmp($childs[$j]["text"], $childs[$j + 1]["text"]) > 0){
        $time = $childs[$j]["text"];
        $childs[$j]["text"] = $childs[$j + 1]["text"];
        $childs[$j + 1]["text"] = $time;

        $time = $childs[$j]["id"];
        $childs[$j]["id"] = $childs[$j + 1]["id"];
        $childs[$j + 1]["id"] = $time;
      };
    };
  };
  return $childs;
};

function show_page() {
  global $data, $path, $style, $session_id, $header, $_POST;

  if (!isset($data["type"])) {
    header("Location: index.php?trg=main&sid=".$session_id);
    die();
  }

  if (!isset($_GET["act"])) $_GET["act"] = "page";
  if (!isset($_GET["idesc"])) $_GET["idesc"] = "false";
  
  switch ($_GET["act"]) {
    case "userdata": //list of users
      if (!isset($_GET["uid"])) $_GET["uid"]="";
      
      switch (substr($_GET["uid"], 0, 1)) {
        case "c":
          $_GET["uid"] = substr($_GET["uid"], 1);
          $contents = getClassData("");
          break;
        case "a":
          $_GET["uid"] = substr($_GET["uid"], 1);
          getData("SELECT * FROM class_attr ".
            "WHERE attr_id = ".$_GET["uid"].
            ";", $contodata); //current class
          $ID[0]=$contodata["class_id"]; $ID[1]=$contodata["attr_id"];
          if ($ID[1] != "")
            $contents = getAttrData($ID, "");
          else
            $contents = '<?xml version="1.0" encoding="utf-8" ?><Info InfoId="sr" Name="" Reload="0"></Info>';
          break;
        case "o":
          $_GET["uid"] = substr($_GET["uid"], 1);
          $contents = getOntoData("");
          break;
        default :
          $contents = '<?xml version="1.0" encoding="utf-8" ?><Info InfoId="sr" Name="" Reload="0"></Info>';
          break;
      }
      header ($header["xml"]);
      break;
    case "userlist": //user's data
      $filename = $path[$style]["tpl"]."/termsearcher.xml";
      $fp = fopen($filename, "r");
      $contents = fread($fp, filesize ($filename));
      fclose ($fp);

      preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
      $rep_cont = "";
      if (!isset($_GET["uid"])) $_GET["uid"] = "0";
      if (!isset($_GET["stext"]))$_GET["stext"] = "";

      switch ($_GET["uid"]) {
        case "classes":
          $_GET["stext"] = utf8Encode($_GET["stext"]);
          $_GET["stext"] = str_replace("'","''", $_GET["stext"]);
          if ($_GET["idesc"] == "true")
            $i_max = getMultData("SELECT * FROM term t, class c WHERE c.term_id=t.id and ((t.text LIKE '%".$_GET["stext"]."%') or (t.description LIKE '%".$_GET["stext"]."%'))", $usersdata);
          else
            $i_max = getMultData("SELECT * FROM term t, class c WHERE c.term_id=t.id and t.text LIKE '%".$_GET["stext"]."%'", $usersdata);
          if ($i_max > 50)
            $i_max = 50;
          //User Data
          $usersdata = my_sort($usersdata, $i_max);
          for ($i = 0; $i < $i_max; $i++) {
            $tmp_cont = str_replace("%CLASS_ID%", "c".$usersdata[$i]["id"], $matches[1]);
            $tmp_cont = str_replace("%IMG_TYPE%", "class", $tmp_cont);
            $tmp_cont = str_replace("%CLASS_NM%", $usersdata[$i]["text"], $tmp_cont);
            $tmp_cont = str_replace("%CLASS_LN%", "index.php?trg=termsearcher&amp;act=userdata&amp;sid=".
              $session_id."&amp;uid="."c".$usersdata[$i]["id"], $tmp_cont);
            $tmp_cont = str_replace("%CHILD_LN%", "", $tmp_cont);
            $rep_cont .= $tmp_cont;
          }
          break;
        case "attrs":
          $_GET["stext"] = utf8Encode($_GET["stext"]);
          $_GET["stext"] = str_replace("'","''", $_GET["stext"]);
          if ($_GET["idesc"] == "true")
            $i_max = getMultData("SELECT * FROM term t, attr a WHERE a.term_id=t.id and (t.text LIKE '%".$_GET["stext"]."%' or t.description LIKE '%".$_GET["stext"]."%')", $usersdata);
          else
            $i_max = getMultData("SELECT * FROM term t, attr a WHERE a.term_id=t.id and t.text LIKE '%".$_GET["stext"]."%'", $usersdata);
          if ($i_max > 50)
            $i_max = 50;
          //User Data
          $usersdata = my_sort($usersdata, $i_max);
          for ($i = 0; $i < $i_max; $i++) {
            $tmp_cont = str_replace("%CLASS_ID%", "a".$usersdata[$i]["id"], $matches[1]);
            $tmp_cont = str_replace("%IMG_TYPE%", "attr", $tmp_cont);
            $tmp_cont = str_replace("%CLASS_NM%", $usersdata[$i]["text"], $tmp_cont);
            $tmp_cont = str_replace("%CLASS_LN%", "index.php?trg=termsearcher&amp;act=userdata&amp;sid=".$session_id."&amp;uid="."a".$usersdata[$i]["id"], $tmp_cont);
            $tmp_cont = str_replace("%CHILD_LN%", "", $tmp_cont);
            $rep_cont .= $tmp_cont;
          }
          break;

        case "ontos":
          $_GET["stext"] = utf8Encode($_GET["stext"]);
          $_GET["stext"] = str_replace("'","''", $_GET["stext"]);
          if ($_GET["idesc"] == "true")
            $i_max = getMultData("SELECT * FROM term t, ontology o WHERE o.term_id=t.id and (t.text LIKE '%".$_GET["stext"]."%' or t.description LIKE '%".$_GET["stext"]."%')", $usersdata);
          else
            $i_max = getMultData("SELECT * FROM term t, ontology o WHERE o.term_id=t.id and t.text LIKE '%".$_GET["stext"]."%'", $usersdata);
          if ($i_max > 50)
            $i_max = 50;
          //User Data
          $usersdata = my_sort($usersdata, $i_max);
          for ($i = 0; $i < $i_max; $i++) {
            $tmp_cont = str_replace("%CLASS_ID%", "o".$usersdata[$i]["id"], $matches[1]);
            $tmp_cont = str_replace("%IMG_TYPE%", "ontology", $tmp_cont);
            $tmp_cont = str_replace("%CLASS_NM%", $usersdata[$i]["text"], $tmp_cont);
            $tmp_cont = str_replace("%CLASS_LN%", "index.php?trg=termsearcher&amp;act=userdata&amp;sid=".$session_id."&amp;uid=o".$usersdata[$i]["id"], $tmp_cont);
            $tmp_cont = str_replace("%CHILD_LN%", "", $tmp_cont);
            $rep_cont .= $tmp_cont;
          }
          break;

        case "sr":
          $_GET["stext"] = utf8Encode($_GET["stext"]);
          $_GET["stext"] = str_replace("'","''", $_GET["stext"]);

          $tmp_cont = str_replace("%CLASS_ID%", "classes", $matches[1]);
          $tmp_cont = str_replace("%IMG_TYPE%", "folder", $tmp_cont);
          if ($_GET["idesc"] == "true")
            $i_max = getMultData("SELECT * FROM term t, class c WHERE c.term_id=t.id and ((t.text LIKE '%".$_GET["stext"]."%') or (t.description LIKE '%".$_GET["stext"]."%'))", $usersdata);
          else
            $i_max = getMultData("SELECT * FROM term t, class c WHERE c.term_id=t.id and t.text LIKE '%".$_GET["stext"]."%'", $usersdata);
          if ($i_max > 50)
            $tmp_cont = str_replace("%CLASS_NM%", getMessage("Classes (Only first 50 are shown)"), $tmp_cont);
          else
            $tmp_cont = str_replace("%CLASS_NM%", getMessage("Classes")." (".$i_max.")", $tmp_cont);
          
          $tmp_cont = str_replace("%CLASS_LN%", "index.php?trg=termsearcher&amp;act=userdata&amp;sid=".
            $session_id."&amp;uid=_classes&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);
          $data = Array();
          
          if ((getdata("SELECT * FROM term t, class c WHERE c.term_id=t.id and t.text LIKE '%".str_replace("'","''", utf8Encode($_GET["stext"]))."%'", $data) == 1) && ($_GET["idesc"] == "false"))
            $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=termsearcher&amp;act=userlist&amp;sid=".
              $session_id."&amp;uid=classes&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);

          if ((getdata("SELECT * FROM term t, class c WHERE c.term_id=t.id and (t.text LIKE '%".str_replace("'","''", utf8Encode($_GET["stext"]))."%' or t.description LIKE '%".str_replace("'","''", utf8Encode($_GET["stext"]))."%')", $data) == 1) && ($_GET["idesc"] == "true"))
            $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=termsearcher&amp;act=userlist&amp;sid=".
              $session_id."&amp;uid=classes&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);
            
          $tmp_cont = str_replace("%CHILD_LN%", "", $tmp_cont);

          $rep_cont = $tmp_cont;

          $tmp_cont = str_replace("%CLASS_ID%", "attrs", $matches[1]);
          $tmp_cont = str_replace("%IMG_TYPE%", "folder", $tmp_cont);

          if ($_GET["idesc"] == "true")
            $i_max = getMultData("SELECT * FROM term t, attr a WHERE a.term_id=t.id and (t.text LIKE '%".$_GET["stext"]."%' or t.description LIKE '%".$_GET["stext"]."%')", $usersdata);
          else
            $i_max = getMultData("SELECT * FROM term t, attr a WHERE a.term_id=t.id and t.text LIKE '%".$_GET["stext"]."%'", $usersdata);
          if ($i_max > 50)
            $tmp_cont = str_replace("%CLASS_NM%", getMessage("Attributes (Only first 50 are shown)"), $tmp_cont);
          else
            $tmp_cont = str_replace("%CLASS_NM%", getMessage("Attributes")." (".$i_max.")", $tmp_cont);


          $tmp_cont = str_replace("%CLASS_LN%", "index.php?trg=termsearcher&amp;act=userdata&amp;sid=".
            $session_id."&amp;uid=_attrs&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);

          if ((getdata("SELECT * FROM term t, attr a WHERE a.term_id=t.id and t.text LIKE '%".str_replace("'","''", utf8Encode($_GET["stext"]))."%'", $data) == 1) && ($_GET["idesc"] == "false"))
            $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=termsearcher&amp;act=userlist&amp;sid=".
              $session_id."&amp;uid=attrs&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);

          if ((getdata("SELECT * FROM term t, attr a WHERE a.term_id=t.id and (t.text LIKE '%".str_replace("'","''", utf8Encode($_GET["stext"]))."%' or t.description LIKE '%".str_replace("'","''", utf8Encode($_GET["stext"]))."%')", $data) == 1) && ($_GET["idesc"] == "true"))
            $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=termsearcher&amp;act=userlist&amp;sid=".
              $session_id."&amp;uid=attrs&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);

          $tmp_cont = str_replace("%CHILD_LN%", "", $tmp_cont);

          $rep_cont .= $tmp_cont;

          $tmp_cont = str_replace("%CLASS_ID%", "ontos", $matches[1]);
          $tmp_cont = str_replace("%IMG_TYPE%", "folder", $tmp_cont);

          if ($_GET["idesc"] == "true")
            $i_max = getMultData("SELECT * FROM term t, ontology o WHERE o.term_id=t.id and (t.text LIKE '%".$_GET["stext"]."%' or t.description LIKE '%".$_GET["stext"]."%')", $usersdata);
          else
            $i_max = getMultData("SELECT * FROM term t, ontology o WHERE o.term_id=t.id and t.text LIKE '%".$_GET["stext"]."%'", $usersdata);
          if ($i_max > 50)
            $tmp_cont = str_replace("%CLASS_NM%", getMessage("Ontologies (Only first 50 are shown)"), $tmp_cont);
          else
            $tmp_cont = str_replace("%CLASS_NM%", getMessage("Ontologies")." (".$i_max.")", $tmp_cont);


          $tmp_cont = str_replace("%CLASS_LN%", "index.php?trg=termsearcher&amp;act=userdata&amp;sid=".
            $session_id."&amp;uid=_ontos&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);

          if ((getdata("SELECT * FROM term t, ontology o WHERE o.term_id=t.id and t.text LIKE '%".str_replace("'","''", utf8Encode($_GET["stext"]))."%'", $data) == 1) && ($_GET["idesc"] == "false"))
            $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=termsearcher&amp;act=userlist&amp;sid=".
              $session_id."&amp;uid=ontos&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);

          if ((getdata("SELECT * FROM term t, ontology o WHERE o.term_id=t.id and (t.text LIKE '%".str_replace("'","''", utf8Encode($_GET["stext"]))."%' or t.description LIKE '%".str_replace("'","''", utf8Encode($_GET["stext"]))."%')", $data) == 1) && ($_GET["idesc"] == "true"))
            $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=termsearcher&amp;act=userlist&amp;sid=".
              $session_id."&amp;uid=ontos&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);

          $tmp_cont = str_replace("%CHILD_LN%", "", $tmp_cont);

          $rep_cont .= $tmp_cont;
          break;
        default: //including 0
          
          $tmp_cont = str_replace("%CLASS_ID%", "sr", $matches[1]);
          $tmp_cont = str_replace("%IMG_TYPE%", "folder", $tmp_cont);
          $tmp_cont = str_replace("%CLASS_LN%", "index.php?trg=termsearcher&amp;act=userdata&amp;sid=".
            $session_id."&amp;uid=sr&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);
          $tmp_cont = str_replace("%CHILD_LN%", "index.php?trg=termsearcher&amp;act=userlist&amp;sid=".
            $session_id."&amp;uid=sr&amp;stext=".$_GET["stext"]."&amp;idesc=".$_GET["idesc"], $tmp_cont);
          $_GET["stext"] = utf8Encode($_GET["stext"]);
/*    
    $tmpfile = fopen("test.txt","w");
    $fp = fwrite($tmpfile, $_GET["stext"]);
    fclose($tmpfile);
*/          
          $tmp_cont = str_replace("%CLASS_NM%", getMessage("Search Results for ").$_GET["stext"], $tmp_cont);
          $rep_cont = $tmp_cont;
          break;        
      }
      
      $contents = str_replace($matches[0], $rep_cont, $contents);
      header ($header["xml"]);
      break;
    default: //including "page"
      $filename = $path[$style]["tpl"]."/termsearcher.htm";
      $fp = fopen($filename, "r");
      $contents = fread($fp, filesize ($filename));
      fclose ($fp);

      $contents = str_replace("%ROOT_CSS%", $path[$style]["css"], $contents);
      $contents = str_replace("%ROOT_IMG%", $path[$style]["img"], $contents);
      $contents = str_replace("%ROOT_JAVA%", $path[$style]["java"], $contents);
      $contents = str_replace("%LOGOUT%", getMessage("Logout"), $contents);
      $contents = str_replace("%MAINMENU%", getMessage("Main menu"), $contents);
      $contents = str_replace("%LOAD%", getMessage("Loading"), $contents);
    
      $contents = str_replace("%TERMSEARCHER%", getMessage("Term searcher"), $contents);
      $contents = str_replace("%SEARCH%", getMessage("Search"), $contents);
      $contents = str_replace("%TERM:%", getMessage("Term:"), $contents);
      $contents = str_replace("%INCLUDEDESCRIPTION%", getMessage("Include Description"), $contents);
      
      $contents = str_replace("%RESULTS:%", "", $contents);
      $contents = str_replace("%RESULTTERMS%", "", $contents);
      
      $contents = str_replace("%SID%", $session_id, $contents);
      header ($header["html"]);
      break;
  }
  header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
  header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
  header ("Pragma: no-cache");                          // HTTP/1.0

  print ($contents);
}



function getClassData($comment) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language;
  $filename = $path[$style]["tpl"]."/classdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
  preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
  $rep_cont = "";
  //print("AAA");
  getData("SELECT class.onto_id as onto_id, term.id as termID, term.text as name, term.description as description ".
          "FROM class, term ".
          "WHERE class.id = ".$_GET["uid"].
          " AND class.term_id = term.id AND term.lang_id = ".$language["content"].
          ";",
          $ontodata);
  getData("SELECT term.text as name FROM ontology, term ".
          "WHERE ontology.id = ".$ontodata["onto_id"].
          " AND ontology.term_id = term.id AND term.lang_id = ".$language["content"].
          ";",
          $pontodata);

  $udata = ""; $btns = "";

  $btn1 = str_replace($optmatches[0], "", $datmatches[1]);
  $btn1 = str_replace("%TYPE%", "Button", $btn1);
  $btn1 = str_replace("%NODE_ID%", "", $btn1);
  $btn1 = str_replace("%NODE_TL%", getMessage("Ontologies"), $btn1);
  $btn1 = str_replace("%VALUE%", "'wd_tree', 'index.php?trg=ontoedit&amp;sid=".
    $session_id."&amp;act=ontolist&amp;sel=".$ontodata["onto_id"]."'", $btn1);
  $btn2 = str_replace($optmatches[0], "", $datmatches[1]);
  $btn2 = str_replace("%TYPE%", "Button", $btn2);
  $btn2 = str_replace("%NODE_ID%", "", $btn2);
  $btn2 = str_replace("%NODE_TL%", getMessage("VRML"), $btn2);
  $btn2 = str_replace("%VALUE%", "'new_wnd', 'index.php?trg=ontoedit&amp;sid=".
    $session_id."&amp;act=vrml&amp;uid=".$ontodata["onto_id"]."&amp;cid=".$_GET["uid"]."'", $btn2);

  //Div
  $btns = str_replace($datmatches[0], $btn1.$btn2, $matches[1]);
  $btns = str_replace("%GROUP_ID%", "1", $btns);
  $btns = str_replace("%GROUP_TL%", getMessage("Action"), $btns);


  //New name
  //$contents = str_replace("%NAME%", $ontodata["name"], $contents);
  //Name
  $fname = str_replace($optmatches[0], "", $datmatches[1]);
  $fname = str_replace("%TYPE%", "Label", $fname);
  $fname = str_replace("%NODE_ID%", "oname", $fname);
  $fname = str_replace("%NODE_TL%", getMessage("Name"), $fname);
  $fname = str_replace("%VALUE%", $ontodata["name"], $fname);
  //Ontology Name
  $ontolb = str_replace($optmatches[0], "", $datmatches[1]);
  $ontolb = str_replace("%TYPE%", "Label", $ontolb);
  $ontolb = str_replace("%NODE_ID%", "lname", $ontolb);
  $ontolb = str_replace("%NODE_TL%", getMessage("Ontology Name"), $ontolb);
  $ontolb = str_replace("%VALUE%", $pontodata["name"], $ontolb);
  //Description
  $lname = str_replace($optmatches[0], "", $datmatches[1]);
  $lname = str_replace("%TYPE%", "Label", $lname);
  $lname = str_replace("%NODE_ID%", "odescr", $lname);
  $lname = str_replace("%NODE_TL%", getMessage("Description"), $lname);
  $lname = str_replace("%VALUE%", $ontodata["description"], $lname);
  //User
  $udata = str_replace($datmatches[0], $ontolb.$fname.$lname, $matches[1]);
  $udata = str_replace("%GROUP_ID%", "0", $udata);
  $udata = str_replace("%GROUP_TL%", getMessage("Class data"), $udata);
  //print "****".$udata."****";
  return str_replace($matches[0], $udata.$btns, $contents);
}

function getAttrData($ID, $comment) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language;
  $filename = $path[$style]["tpl"]."/attrdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
  preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
  $rep_cont = "";
  //print("AAA");
  getData("SELECT ca.domtype_id, ca.min, ca.max, ca.limited, t.text FROM class_attr ca, domtype d, term t ".
          "WHERE d.term_id=t.id and d.id=ca.domtype_id and ca.attr_id=".$ID[1]." and ca.class_id=".$ID[0].";",
          $domaindata); //domain, min, max, etc.
          
  getData("SELECT term.id as term_id, term.text as name, term.description as description ".
          "FROM attr, term ".
          "WHERE attr.id = ".$ID[1].
          " AND attr.term_id = term.id AND term.lang_id = ".$language["content"].
          ";",
          $ontodata); //name, descr
  getData("SELECT term.text as name, class.onto_id as onto_id FROM class, term ".
          "WHERE class.id = ".$ID[0].
          " AND class.term_id = term.id AND term.lang_id = ".$language["content"].
          ";",
          $contodata); //current class
  getData("SELECT term.text as name FROM ontology, term ".
          "WHERE ontology.id = ".$contodata["onto_id"].
          " AND ontology.term_id = term.id AND term.lang_id = ".$language["content"].
          ";",
          $pontodata); //current ontology

  $udata = "";

    //New name
    //$contents = str_replace("%NAME%", $ontodata["name"], $contents);
    //Class Name
    $classlb = str_replace($optmatches[0], "", $datmatches[1]);
    $classlb = str_replace("%TYPE%", "Label", $classlb);
    $classlb = str_replace("%NODE_ID%", "cname", $classlb);
    $classlb = str_replace("%NODE_TL%", getMessage("Class Name"), $classlb);
    $classlb = str_replace("%VALUE%", $contodata["name"], $classlb);

    //Parent Class Name
    $iclasslb = "";

    //Domain
    $utype = str_replace($optmatches[0], "", $datmatches[1]);
    $utype = str_replace("%TYPE%", "Label", $utype);
    $utype = str_replace("%NODE_ID%", "otype", $utype);
    $utype = str_replace("%NODE_TL%", getMessage("Domain"), $utype);
    $utype = str_replace("%VALUE%", $domaindata["text"], $utype);

    //Ontology Name
    $ontolb = str_replace($optmatches[0], "", $datmatches[1]);
    $ontolb = str_replace("%TYPE%", "Label", $ontolb);
    $ontolb = str_replace("%NODE_ID%", "lname", $ontolb);
    $ontolb = str_replace("%NODE_TL%", getMessage("Ontology Name"), $ontolb);
    $ontolb = str_replace("%VALUE%", $pontodata["name"], $ontolb);
    //Name
    $fname = str_replace($optmatches[0], "", $datmatches[1]);
    $fname = str_replace("%TYPE%", "Label", $fname);
    $fname = str_replace("%NODE_ID%", "oname", $fname);
    $fname = str_replace("%NODE_TL%", getMessage("Name"), $fname);
    $fname = str_replace("%VALUE%", $ontodata["name"], $fname);
    //Description
    $lname = str_replace($optmatches[0], "", $datmatches[1]);
    $lname = str_replace("%TYPE%", "Label", $lname);
    $lname = str_replace("%NODE_ID%", "odescr", $lname);
    $lname = str_replace("%NODE_TL%", getMessage("Description"), $lname);
    $lname = str_replace("%VALUE%", $ontodata["description"], $lname);
    //Label
    $lablttt = str_replace($optmatches[0], "", $datmatches[1]);
    $lablttt = str_replace("%TYPE%", "Label", $lablttt);
    $lablttt = str_replace("%NODE_ID%", "", $lablttt);
    if ($comment != "") $lablttt = str_replace("%NODE_TL%", getMessage("Error"), $lablttt);
    else $lablttt = str_replace("%NODE_TL%", "", $lablttt);
    $lablttt = str_replace("%VALUE%", $comment, $lablttt);
    //button
    $ubutn = "";
    $ubutn = str_replace($optmatches[0], "", $datmatches[1]);
    $ubutn = str_replace("%TYPE%", "Label", $ubutn);
    $ubutn = str_replace("%NODE_ID%", "obutn", $ubutn);
    $ubutn = str_replace("%NODE_TL%", "", $ubutn);
    $ubutn = str_replace("%VALUE%", "", $ubutn);
    //User
    $udata = str_replace($datmatches[0], $classlb.$iclasslb.$ontolb.$fname.$lname.$utype.$lablttt.$ubutn, $matches[1]);
    $udata = str_replace("%GROUP_ID%", "0", $udata);
    $udata = str_replace("%GROUP_TL%", getMessage("Attribute data"), $udata);
    //print "****".$udata."****";

  return str_replace($matches[0], $udata, $contents);
}


function getOntoData($comment) {
  global $_GET, $path, $style, $session_id, $data, $language;
  $filename = $path[$style]["tpl"]."/ontodata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
  preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
  $rep_cont = "";
  getData("SELECT term.text as name, term.description as description, ontology.type_id as type ".
          "FROM ontology, term ".
          "WHERE ontology.id = ".$_GET["uid"].
          " AND ontology.term_id = term.id AND term.lang_id = ".$language["content"].";",
          $ontodata);
  $i_max = getMultData("SELECT ontotype.id as id, term.text as name FROM ontotype, term ".
          "WHERE ontotype.term_id = term.id AND term.lang_id = ".$language["content"].";",
          $ontotypes);

  $udata = ""; $btns = "";
  $btn1 = str_replace($optmatches[0], "", $datmatches[1]);
  $btn1 = str_replace("%TYPE%", "Button", $btn1);
  $btn1 = str_replace("%NODE_ID%", "", $btn1);
  $btn1 = str_replace("%NODE_TL%", getMessage("Taxonomy"), $btn1);
  $btn1 = str_replace("%VALUE%", "'wd_tree', 'index.php?trg=ontoedit&amp;act=tax&amp;sid=".$session_id."&amp;uid=".$_GET["uid"]."'", $btn1);
  //VRML
  $btn2 = str_replace($optmatches[0], "", $datmatches[1]);
  $btn2 = str_replace("%TYPE%", "Button", $btn2);
  $btn2 = str_replace("%NODE_ID%", "", $btn2);
  $btn2 = str_replace("%NODE_TL%", getMessage("VRML"), $btn2);
  $btn2 = str_replace("%VALUE%", "'new_wnd', 'index.php?trg=ontoedit&amp;act=vrml&amp;sid=".$session_id."&amp;uid=".$_GET["uid"]."'", $btn2);
  //Div
  $btns = str_replace($datmatches[0], $btn1.$btn2, $matches[1]);
  $btns = str_replace("%GROUP_ID%", "1", $btns);
  $btns = str_replace("%GROUP_TL%", getMessage("Action"), $btns);

  //New name
  $contents = str_replace("%NAME%", $ontodata["name"], $contents);
  //Name
  $fname = str_replace($optmatches[0], "", $datmatches[1]);
  $fname = str_replace("%TYPE%", "Label", $fname);
  $fname = str_replace("%NODE_ID%", "oname", $fname);
  $fname = str_replace("%NODE_TL%", getMessage("Name"), $fname);
  $fname = str_replace("%VALUE%", $ontodata["name"], $fname);
  //Description
  $lname = str_replace($optmatches[0], "", $datmatches[1]);
  $lname = str_replace("%TYPE%", "Label", $lname);
  $lname = str_replace("%NODE_ID%", "odescr", $lname);
  $lname = str_replace("%NODE_TL%", getMessage("Description"), $lname);
  $lname = str_replace("%VALUE%", $ontodata["description"], $lname);
  //Type
  //options
  $opts = "";
  for ($i = 0; $i < $i_max; $i++) {
    if ($ontodata["type"] == $ontotypes[$i]["id"]) break;
  }
  //type
  $utype = str_replace($optmatches[0], "", $datmatches[1]);
  $utype = str_replace("%TYPE%", "Label", $utype);
  $utype = str_replace("%NODE_ID%", "otype", $utype);
  $utype = str_replace("%NODE_TL%", getMessage("Type"), $utype);
  $utype = str_replace("%VALUE%", $ontotypes[$i]["name"], $utype);
  //User
  $udata = str_replace($datmatches[0], $fname.$lname.$utype, $matches[1]);
  $udata = str_replace("%GROUP_ID%", "0", $udata);
  $udata = str_replace("%GROUP_TL%", getMessage("Ontology data"), $udata);
  //print "****".$udata."****";
  return str_replace($matches[0], $udata.$btns, $contents);
}

?>